﻿using Abp.Authorization;
using Abp.Localization;
using Abp.MultiTenancy;
using pandx.Mulan.Settings;

namespace pandx.Mulan.Authorization
{
    public class AppAuthorizationProvider : AuthorizationProvider
    {
        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ?? context.CreatePermission(AppPermissions.Pages, L("Pages"), L("B"));

            //FlowInstance
            var flowInstances = pages.CreateChildPermission(AppPermissions.Pages_FlowInstances, L("FlowInstances"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Browse, L("FlowInstancesBrowse"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Launch, L("FlowInstancesLaunch"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Export, L("FlowInstancesExport"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Verify, L("FlowInstancesVerify"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Recall, L("FlowInstancesRecall"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Restart, L("FlowInstancesRestart"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Display, L("FlowInstancesDisplay"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Delete, L("FlowInstancesDelete"));
            flowInstances.CreateChildPermission(AppPermissions.Pages_FlowInstances_Working, L("FlowInstancesWorking"));
            
            //Book
            var books = pages.CreateChildPermission(AppPermissions.Pages_Books, L("Books"));
            books.CreateChildPermission(AppPermissions.Pages_Books_Browse, L("BooksBrowse"));
            books.CreateChildPermission(AppPermissions.Pages_Books_Create, L("BooksCreate"));
            books.CreateChildPermission(AppPermissions.Pages_Books_Update, L("BooksUpdate"));
            books.CreateChildPermission(AppPermissions.Pages_Books_Delete, L("BooksDelete"));
            books.CreateChildPermission(AppPermissions.Pages_Books_Export, L("BooksExport"));
            
            var administrator = pages.CreateChildPermission(AppPermissions.Pages_Administration, L("Administration"));
            
            //OrganizationUnit
            var organizationUnits =
                administrator.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits,
                    L("OrganizationUnits"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_Browse,
                L("OrganizationUnitsBrowse"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_Create,
                L("OrganizationUnitsCreate"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_Update,
                L("OrganizationUnitsUpdate"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_Delete,
                L("OrganizationUnitsDelete"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_AddUser,
                L("OrganizationUnitsAddUser"));
            organizationUnits.CreateChildPermission(AppPermissions.Pages_Administration_OrganizationUnits_RemoveUser,
                L("OrganizationUnitsRemoveUser"));
            
            //User
            var users = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Users, L("Users"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Browse, L("UsersBrowse"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Create, L("UsersCreate"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Update, L("UsersUpdate"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Delete, L("UsersDelete"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Unlock, L("UsersUnlock"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Permission, L("UsersPermission"));

            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_ChangePassword, L("UsersChangePassword"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Export, L("UsersExport"));
            users.CreateChildPermission(AppPermissions.Pages_Administration_Users_Import, L("UsersImport"));
            
            //Role
            var roles = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Roles, L("Roles"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Browse, L("RolesBrowse"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Create, L("RolesCreate"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Update, L("RolesUpdate"));
            roles.CreateChildPermission(AppPermissions.Pages_Administration_Roles_Delete, L("RolesDelete"));
            
            //DictionaryCategory
            var dictionaryCategories = administrator.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories, L("DictionaryCategories"));
            dictionaryCategories.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories_Browse, L("DictionaryCategoriesBrowse"));
            dictionaryCategories.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories_Create, L("DictionaryCategoriesCreate"));
            dictionaryCategories.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories_Update, L("DictionaryCategoriesUpdate"));
            dictionaryCategories.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories_Delete, L("DictionaryCategoriesDelete"));
            dictionaryCategories.CreateChildPermission(AppPermissions.Pages_Administration_DictionaryCategories_Export, L("DictionaryCategoriesExport"));
            
            //Dictionary
            var dictionaries = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries, L("Dictionaries"));
            dictionaries.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries_Browse, L("DictionariesBrowse"));
            dictionaries.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries_Create, L("DictionariesCreate"));
            dictionaries.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries_Update, L("DictionariesUpdate"));
            dictionaries.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries_Delete, L("DictionariesDelete"));
            dictionaries.CreateChildPermission(AppPermissions.Pages_Administration_Dictionaries_Export, L("DictionariesExport"));
            
            //FormScheme
            var formSchemes = administrator.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes, L("FormSchemes"));
            formSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes_Browse, L("FormSchemesBrowse"));
            formSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes_Create, L("FormSchemesCreate"));
            formSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes_Update, L("FormSchemesUpdate"));
            formSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes_Delete, L("FormSchemesDelete"));
            formSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FormSchemes_Export, L("FormSchemesExport"));
            
            //FlowScheme
            var flowSchemes = administrator.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes, L("FlowSchemes"));
            flowSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes_Browse, L("FlowSchemesBrowse"));
            flowSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes_Create, L("FlowSchemesCreate"));
            flowSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes_Update, L("FlowSchemesUpdate"));
            flowSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes_Delete, L("FlowSchemesDelete"));
            flowSchemes.CreateChildPermission(AppPermissions.Pages_Administration_FlowSchemes_Export, L("FlowSchemesExport"));
            
            //Audit
            var audits = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Audits, L("Audits"));
            audits.CreateChildPermission(AppPermissions.Pages_Administration_Audits_Browse, L("AuditsBrowse"));
            audits.CreateChildPermission(AppPermissions.Pages_Administration_Audits_Export, L("AuditsExport"));

            //Maintenance
            var maintenances = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Maintenances, L("Maintenances"));
            maintenances.CreateChildPermission(AppPermissions.Pages_Administration_Maintenances_Browse, L("MaintenancesBrowse"));
            maintenances.CreateChildPermission(AppPermissions.Pages_Administration_Maintenances_Export, L("MaintenancesExport"));
            maintenances.CreateChildPermission(AppPermissions.Pages_Administration_Maintenances_Clear, L("MaintenancesClear"));

            //Setting
            var settings = administrator.CreateChildPermission(AppPermissions.Pages_Administration_Settings, L("Settings"));
            settings.CreateChildPermission(AppPermissions.Pages_Administration_Settings_Browse, L("SettingsBrowse"));
            settings.CreateChildPermission(AppPermissions.Pages_Administration_Settings_Save, L("SettingsSave"));

            var files = pages.CreateChildPermission(AppPermissions.Pages_Files, L("Files"));
            files.CreateChildPermission(AppPermissions.Pages_Files_Upload, L("UploadFiles"));
            files.CreateChildPermission(AppPermissions.Pages_Files_Download, L("DownloadFiles"));
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, CoreConsts.LocalizationSourceName);
        }
    }
}

